<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="baidu-site-verification" content="SCzNOI2Wq6" />
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>
        daozhuang
    </title>
    
<link rel="stylesheet" href="/libs/highlight/styles/monokai-sublime.css">

    
<link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">

    
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body id="bodyx">
    <div class="hd posts">
    <a href="/index.html"><i class="fa fa-reply replay-btn" aria-hidden="true"></i></a>
    <div class="post-title">
        <p>
            手写springboot starter组件示例
        </p>
        <hr>
    </div>
    <div class="post-content">
        <h4 id="Spring-Boot-特性："><a href="#Spring-Boot-特性：" class="headerlink" title="Spring Boot 特性："></a>Spring Boot 特性：</h4><p>1.能够快速创建基于Spring的应用程序；<br>2.能供直接使用java main 方法启动内置的Tomcat或者Jetty服务器运行Spring Boot程序；<br>3.提供约定的starter POM来简化Maven的配置，让Maven的配置变得更简单；<br>4.根据项目的Maven依赖配置，Spring Boot自动配置Spring、Spring MVC等等；<br>5.提供了程序的健康监控等功能；<br>6.基本可以完全不使用XML配置文件，采用注解进行配置。</p>
<h4 id="Spring-Boot四大核心："><a href="#Spring-Boot四大核心：" class="headerlink" title="Spring Boot四大核心："></a>Spring Boot四大核心：</h4><p>1.自动配置：针对很多Spring应用程序和常用的应用功能，Spring Boot能自动提供相关配置；<br>2.starter 组件：starter 是 SpringBoot 的一个重要的组成部分，它相当于一个集成的模块；<br>3.Actuator：让你能深入运行中的Spring Boot应用程序，监控程序的内部信息；<br>4.命令行界面：这个为Spring Boot的可选特性，主要是针对Groovy的。</p>
<h1 id="springboot-starter-组件"><a href="#springboot-starter-组件" class="headerlink" title="springboot starter 组件"></a>springboot starter 组件</h1><p>starter 是 SpringBoot 的一个重要的组成部分，它相当于一个集成的模块，比如你想用 Mybatis 和 lombok，但是在 pom 文件中需要写两个依赖，如果你将他们集成为一个 starter（或者将更多你需要的依赖集成进去），那么你只需要在 pom 文件中写一个 starter 依赖就可以了，这对于一个可复用模块的开发和维护都极为有利。</p>
<p>同时，在 maven 中引入 starter 依赖之后，SpringBoot 就能自动扫描到要加载的信息并启动相应的默认配置，它遵循“约定大于配置”的理念。</p>
<h6 id="项目结构"><a href="#项目结构" class="headerlink" title="项目结构"></a>项目结构</h6><p>Spring 官方建议自定义的 starter 使用 xxx-spring-boot-starter 命名规则，以区分 SpringBoot 生态提供的 starter。<br><img src="https://img-blog.csdnimg.cn/2020072010274897.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzg2NTMxMg==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<h4 id="在-pom-文件中加入实现-starter-所需要的依赖。"><a href="#在-pom-文件中加入实现-starter-所需要的依赖。" class="headerlink" title="在 pom 文件中加入实现 starter 所需要的依赖。"></a>在 pom 文件中加入实现 starter 所需要的依赖。</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 编写 starter 组件必要的依赖--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;2.1.6.RELEASE&lt;&#x2F;version&gt;</span><br><span class="line">    &lt;!-- 设置依赖传递 为  不传递 --&gt;</span><br><span class="line">    &lt;optional&gt;true&lt;&#x2F;optional&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line">&lt;!-- application.yaml 配置文件的自动提示需要的依赖 --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-configuration-processor&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;2.1.6.RELEASE&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line">&lt;!-- Redis 操作客户端 测试工程使用 --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.redisson&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;redisson&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;3.13.1&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure>
<h4 id="创建配置信息实体类"><a href="#创建配置信息实体类" class="headerlink" title="创建配置信息实体类"></a>创建配置信息实体类</h4><p>application.yaml 配置文件自动提示 的配置信息实体类<br>需要提供  get  set  我这里没有提供</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;</span><br><span class="line"><span class="comment">// @ConfigurationProperties(prefix = "gp.daozhuang") 注解的作用是将相同前缀的配置信息通过配置项名称映射成实体类。</span></span><br><span class="line"><span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"gp.daozhuang"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RedissonProperties</span> </span>&#123;</span><br><span class="line">	<span class="comment">// 需要提供  get  set  我这里没有提供</span></span><br><span class="line">    <span class="keyword">private</span> String host = <span class="string">"localhost"</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> port = <span class="number">6379</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> timeout; <span class="comment">//超时时间</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span> ssl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>编写 META-INF 文件下 文件： additional-spring-configuration-metadata.json</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"properties"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"gp.daozhuang.host"</span>,</span><br><span class="line">      <span class="attr">"type"</span>: <span class="string">"java.lang.String"</span>,</span><br><span class="line">      <span class="attr">"description"</span>: <span class="string">"redis的服务器地址"</span>,</span><br><span class="line">      <span class="attr">"defaultValue"</span>: <span class="string">"localhost"</span></span><br><span class="line">    &#125;,&#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"gp.daozhuang.port"</span>,</span><br><span class="line">      <span class="attr">"type"</span>: <span class="string">"java.lang.Integer"</span>,</span><br><span class="line">      <span class="attr">"description"</span>: <span class="string">"redis服务器的端口"</span>,</span><br><span class="line">      <span class="attr">"defaultValue"</span>: <span class="number">6379</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="创建配置类"><a href="#创建配置类" class="headerlink" title="创建配置类"></a>创建配置类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.EnableConfigurationProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span>: DaoZhuang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span>: 2020/7/17</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="comment">// 使使用 @ConfigurationProperties 注解的类生效。 在上一步中使用到了</span></span><br><span class="line"><span class="meta">@EnableConfigurationProperties</span>(RedissonProperties<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line"><span class="class">//@<span class="title">ConditionalOnProperty</span></span></span><br><span class="line"><span class="class">@<span class="title">Configuration</span></span></span><br><span class="line"><span class="class"><span class="title">public</span> <span class="title">class</span> <span class="title">RedisConfiguration</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> RedisClient <span class="title">redisClient</span><span class="params">(RedissonProperties redissonProperties)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> RedisClient(redissonProperties.getHost(),redissonProperties.getPort());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="Conditional-注解来控制Configuration是否生效"><a href="#Conditional-注解来控制Configuration是否生效" class="headerlink" title="@Conditional 注解来控制Configuration是否生效"></a>@Conditional 注解来控制Configuration是否生效</h6><p>@Conditional是Spring4新提供的注解，它的作用是按照一定的条件进行判断，满足条件给容器注册bean。<br>以下是所用的  Conditional 注解  用以判断条件  本次工程没有用到<br><img src="https://img-blog.csdnimg.cn/20200720110943201.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzg2NTMxMg==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h4 id="编写具体实现类"><a href="#编写具体实现类" class="headerlink" title="编写具体实现类"></a>编写具体实现类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.redisson.Redisson;</span><br><span class="line"><span class="keyword">import</span> org.redisson.api.RList;</span><br><span class="line"><span class="keyword">import</span> org.redisson.api.RedissonClient;</span><br><span class="line"><span class="keyword">import</span> org.redisson.config.Config;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span>: DaoZhuang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span>: 2020/7/17</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RedisClient</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String host;</span><br><span class="line">    <span class="keyword">private</span> Integer port;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">RedisClient</span><span class="params">(String host,Integer port)</span></span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.host = host;</span><br><span class="line">        <span class="keyword">this</span>.port = port;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">redisCli</span><span class="params">()</span></span>&#123;</span><br><span class="line">        Config config=<span class="keyword">new</span> Config();</span><br><span class="line">        config.useSingleServer().</span><br><span class="line">                setAddress(<span class="string">"redis://"</span>+ host +<span class="string">":"</span> + port).</span><br><span class="line">                setConnectTimeout(<span class="number">10000</span>);</span><br><span class="line">        RedissonClient redissonClient = Redisson.create(config);</span><br><span class="line">        RList&lt;Object&gt; nameList = redissonClient.getList(<span class="string">"nameList"</span>);</span><br><span class="line">        nameList.add(<span class="string">"ddd"</span>);</span><br><span class="line">        </span><br><span class="line">        nameList.forEach(System.out::println);</span><br><span class="line">        </span><br><span class="line">        redissonClient.shutdown();</span><br><span class="line">        <span class="keyword">return</span> <span class="string">""</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="在测试工程-springautobeandemo-中-测试"><a href="#在测试工程-springautobeandemo-中-测试" class="headerlink" title="在测试工程 springautobeandemo 中  测试"></a>在测试工程 springautobeandemo 中  测试</h4><p>在pom文件中引入上边的工程<br>注意   引入的依赖</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span>: DaoZhuang</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span>: 2020/7/17</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RestController</span>(<span class="string">""</span>)</span><br><span class="line"><span class="meta">@RequestMapping</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RedisTest</span> </span>&#123;</span><br><span class="line">	<span class="comment">// 直接注入  自己编写的实现类  直接使用  会有默认的配置</span></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> RedisClient redisClient;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping</span>(<span class="string">"index"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">index</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> redisClient.redisCli();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在 application.yaml 文件中  配置自己定义的  配置项</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">gp:</span></span><br><span class="line">  <span class="attr">daozhuang:</span></span><br><span class="line">    <span class="attr">host:</span></span><br><span class="line">    <span class="attr">port:</span></span><br><span class="line">    <span class="attr">ssl:</span></span><br><span class="line">    <span class="attr">timeout:</span></span><br></pre></td></tr></table></figure>


    </div>

    
        <hr class="fhr">
        <div id="vcomments"></div>
    
</div>
    <div class="footer" id="footer">
    <p>Copyright © 2020 <a class="flink" href="https://hexo.io" target="_blank" rel="noopener">Hexo</a>-<a class="flink" href="https://github.com/sanjinhub/hexo-theme-geek" target="_blank" rel="noopener">Geek</a>.
        <label class="el-switch el-switch-green el-switch-sm" style="vertical-align: sub;">
            <input type="checkbox" name="switch" id="update_style">
            <span class="el-switch-style"></span>
        </label>
<!--         <script type="text/javascript">
        var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
        document.write(unescape("%3Cspan id='cnzz_stat_icon_1278548644'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/stat.php%3Fid%3D1278548644%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
        </script> -->
    </p>
</div>
<input type="hidden" id="web_style" value="black">
<input type="hidden" id="valine_appid" value="c994h5wXLa8fSMa424BTyVhs-gzGzoHsz">
<input type="hidden" id="valine_appKey" value="4tFbwCi4sYo3yRFgiNFpQd4N">

<script src="/libs/jquery.min.js"></script>


<script src="/libs/highlight/highlight.pack.js"></script>

<script src='//cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>

<script src="/js/js.js"></script>

<style type="text/css">
.v * {
    color: #698fca;
}

.v .vlist .vcard .vhead .vsys {
    color: #3a3e4a;
}

.v .vlist .vcard .vh .vmeta .vat {
    color: #638fd5;
}

.v .vlist .vcard .vhead .vnick {
    color: #6ba1ff;
}

.v a {
    color: #8696b1;
}

.v .vlist .vcard .vhead .vnick:hover {
    color: #669bfc;
}
</style>
</body>

</html>